package com.liu.dw.config;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

import java.io.Serializable;

/**
 * @Description: 通用http响应模板
 */

@ApiModel(value = "通用http响应模板", description = "返回给前端的实体")
public class ApiResult<T> implements Serializable {
    public static ApiResult success() {
        return new ApiResult("200", "success");
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>("200", "服务器处理成功", data);
    }
    public static <T> ApiResult<T> fail(T data) {
        return new ApiResult<>("503", "数据未完全导入成功", data);
    }

    public static ApiResult fail(com.liu.dw.config.ResponseStatus response) {
        return new ApiResult(response.value(), response.reason());
    }

    public static ApiResult fail(String code, String msg) {
        return new ApiResult(code, msg);
    }

    public static ApiResult internalError(String reason) {
        return new ApiResult("500", reason);
    }

    public static ApiResult error() {
        return new ApiResult("500", "error", null);
    }


    public ApiResult() {
    }

    public ApiResult(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public ApiResult(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     *
     */
    private static final long serialVersionUID = -2402122704294916086L;

    @ApiModelProperty("状态码")
    @Getter
    private String code;
    @Getter
    @ApiModelProperty("后端返回的信息")
    private String message;
    @Getter
    @ApiModelProperty("后端返回的数据")
    private T data;

    public static ApiResult unAuthorization() {
        return new ApiResult("401", "未认证", null);
    }

    public static ApiResult accessDenied() {
        return new ApiResult("403", "权限不足", null);
    }

    public static ApiResult tokenExpiration() {
        return new ApiResult("405", "token过期", null);
    }



}